package com.phpxiu.app.view.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;

/**
 * 列表内容为空时显示的View
 * Created by 吴金洲 on 2016/7/7
 */
public class ListEmptyView extends RelativeLayout{
    private boolean isEmptyState=false;//显示空内容状态，有两种状态，1.显示加载进度状态，初始化时显示。2.请求加载完成，但是列表为空
    private RelativeLayout loadingView;
    private LinearLayout emptyView;
    private ImageView loading;
    private FrescoImageView emptyImageView;
    private TextView emptyMsgView;
    private AnimationDrawable loadingAni;
    public ListEmptyView(Context context) {
        super(context);
        init(context);
    }

    public ListEmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ListEmptyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(21)
    public ListEmptyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    /**
     * 初始化
     */
    public void init(Context context){
        addView(LayoutInflater.from(context).inflate(R.layout.list_empty_view, null), new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        loadingView=(RelativeLayout)findViewById(R.id.loading_view);
        emptyView=(LinearLayout)findViewById(R.id.empty_view);
        loading=(ImageView)findViewById(R.id.loading);
        loadingAni=(AnimationDrawable) loading.getBackground();
        loadingAni.start();
        emptyImageView=(FrescoImageView)findViewById(R.id.empty_image_view);
        emptyImageView.setImageURI(Uri.parse("res:///" + R.drawable.list_empty_icon));
        emptyMsgView=(TextView)findViewById(R.id.empty_msg_view);
    }

    /**
     * 加载完成，更改视图
     */
    public void loaded(){
        isEmptyState=true;
        if(loadingAni!=null&&loadingAni.isRunning()){
            loadingAni.stop();
        }
        if(loadingView.getVisibility()==VISIBLE){
            loadingView.setVisibility(INVISIBLE);
        }
        if(emptyView.getVisibility()!=VISIBLE){
            emptyView.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置空置原因
     * @param msg
     */
    public void setMsg(String msg){
        if(emptyMsgView!=null){
            emptyMsgView.setText(msg);
        }
    }
}
